package com.simdo.g768;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.simdo.g768.Util.AppLog;
import com.simdo.g768.Util.PrefUtil;

public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Window window = this.getWindow();
        //window.requestFeature(Window.FEATURE_NO_TITLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        );
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);

        initView();
    }

    private long backPressed;
    @Override
    public void onBackPressed() {
        if (backPressed + 1000 > System.currentTimeMillis()) {
            super.onBackPressed();
            finish();
        } else {
            showToast(getString(R.string.click_again));
            backPressed = System.currentTimeMillis();
        }
    }

    private void initView() {
        findViewById(R.id.login_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftKeyBoard(view);
            }
        });

        final EditText ed_login_name = findViewById(R.id.ed_login_name);
        ed_login_name.setText(PrefUtil.build().getString("login_name", ""));
        SpannableString s = new SpannableString(getString(R.string.input_user));
        AbsoluteSizeSpan textSize = new AbsoluteSizeSpan(14,true);
        s.setSpan(textSize,0,s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ed_login_name.setHint(s);
        final EditText ed_login_password = findViewById(R.id.ed_login_password);
        ed_login_password.setHint(fromHtml(getString(R.string.input_password)));
        ed_login_password.setText(PrefUtil.build().getString("login_password", ""));
        ImageView iv_login_see = findViewById(R.id.iv_login_see);
        final boolean[] isPasswordType = {true};
        iv_login_see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isPasswordType[0] = !isPasswordType[0];
                ed_login_password.setInputType(isPasswordType[0] ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
                iv_login_see.setImageResource(isPasswordType[0] ? R.mipmap.unsee_icon : R.mipmap.see_icon);
            }
        });
        findViewById(R.id.tv_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String login_name = ed_login_name.getText().toString();
                if (login_name.isEmpty()) {
                    showToast(getString(R.string.please_input_user));
                    return;
                }
                String login_password = ed_login_password.getText().toString();
                if (login_password.isEmpty()) {
                    showToast(getString(R.string.please_input_pwd));
                    return;
                }
                if (!login_name.equals("admin") || !login_password.equals("admin")){
                    showToast(getString(R.string.user_or_pwd_err));
                    return;
                }
                PrefUtil.build().putString("login_name", login_name);
                PrefUtil.build().putString("login_password", login_password);

                startActivity(new Intent(LoginActivity.this, MainActivity.class));

                finish();
            }
        });
    }
}